<template>
    <div>
        <div class="postcomment">
            <h4>提交评论</h4>
            <p class="line"></p>
            <textarea v-model="postcontent" cols="5" rows="5" placeholder="请输入评论信息"></textarea>
            <mt-button type="primary" size="large" @click="submitComment">发表</mt-button>
            <p class="line"></p>
        </div>
        <div class="commentlist">
            <h4>评论列表</h4>
            <p class="line"></p>
            <div v-for="(item,index) in list">
                <div class="title">
                    <span>第{{index + 1}}楼:</span>
                    <span>用户：{{item.user_name}}</span>
                    <span>发表时间：{{item.add_time | datefmt('YYYY-MM-DD')}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell" v-text="item.content"></li>
                </ul>
            </div>
            <mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
    
        </div>
    </div>
</template>

<script>
export default {
    props: ['newsid'],
    data() {
        return {
            list: [],
            pageindex: 1,
            postcontent: ''
        }
    },
    created() {
        this.getCommentList(this.pageindex)
    },
    methods: {
        getCommentList(pageindex) {
            pageindex = pageindex || 1;
            var url = this.$apidomain + '/api/getcomments/' + this.newsid + '?pageindex=' + pageindex;
            this.$http.get(url).then(res => {
                if (res.body.status != 0) {
                    this.$tip(res.body.message)
                    return
                }
                this.list = this.list.concat(res.body.message)
            })
        },
        getmore() {
            this.pageindex++
            this.getCommentList(this.pageindex)
        },
        submitComment() {
            if(this.postcontent.trim().length <= 0){
                this.$tip('您要评论的内容不能为空')
                return
            }
            var url = this.$apidomain + '/api/postcomment/' + this.newsid
            this.$http.post(url, {content:this.postcontent}, {emulateJSON:true}).then(res=>{
                this.$tip(res.body.message)
                this.postcontent = ''
            })
        }
    }
}
</script>

<style scoped>
h4 {
    margin: 10px 0;
}

.postcomment {
    padding: 0 10px;
}

textarea {
    margin-bottom: 5px;
}

.line {
    border: 1px solid rgba(0, 0, 0, 0.3);
    margin-top: 5px;
}

.commentlist {
    padding: 0 10px;
}

.title {
    padding: 5px;
    color: #6d6d72;
    font-size: 15px;
    background-color: rgba(0, 0, 0, 0.1);
}
</style>